package com.nbpt;

import android.content.Intent;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public  class MenuActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    GridView gridView;
//    String name[]={"苹果","香蕉","樱桃","葡萄","芒果","桔子","梨","菠萝","草莓","西瓜"};
//    int image[]={R.drawable.apple_pic,R.drawable.banana_pic, R.drawable.cherry_pic, R.drawable.grape_pic, R.drawable.mango_pic,R.drawable.orange_pic,R.drawable.pear_pic,R.drawable.pineapple_pic,R.drawable.strawberry_pic,R.drawable.watermelon_pic};
    String name[]={"智能家居","智能商超","天气预报","设置中心","开窗帘","关窗帘"};
    int image[]={R.drawable.icon_home,R.drawable.icon_shopping,R.drawable.icon_weather,R.drawable.icon_settings,R.drawable.ic_launcher_background,R.drawable.ic_launcher_foreground};
//    int color[]={R.color.main_darkblue,R.color.main_orange,R.color.main_orangered,R.color.main_blue};
    String from[]={"image","name"};
    int to[]={R.id.imageview,R.id.textview};
    List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
    String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manu);

        gridView=findViewById(R.id.gridView);

        for (int i=0;i<name.length;i++){
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("name",name[i]);
            map.put("image",image[i]);
//            map.put("color",color[i]);
            data.add(map);
        }
        //        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.girdview_item,name);
        SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.girdview_item,from,to);
        gridView.setAdapter(adapter);
        //绑定监听对象
        gridView.setOnItemClickListener(MenuActivity.this);
        Intent intent=getIntent();
        token=intent.getStringExtra("token");
        Toast.makeText(this, token,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this,"点中了"+name[i],Toast.LENGTH_SHORT).show();
        switch (i){
            case 0:
                Intent intent0=new Intent(this,HomeActivity.class);
                startActivity(intent0);
                break;
            case 1:
                Intent intent1=new Intent(this,ShopActivity.class);
                startActivity(intent1);
                break;
            case 2:
                Intent intent2=new Intent(this,WeatherActivity.class);
                startActivity(intent2);
                break;
            case 3:
                Intent intent3=new Intent(this,SettingActivity.class);
                startActivity(intent3);
                break;
            case 4:
                final JSONObject jsonAccount=new JSONObject();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpURLConnection connection=null;
                        try {
                            URL url=new URL("http://192.168.1.7:9020/iot/class/curtain?action=open&id=2");
                            connection=(HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("POST");
                            connection.setConnectTimeout(5000);
                            connection.setReadTimeout(50000);
                            connection.setRequestProperty("connection","Keep_Alive");
                            connection.setRequestProperty("Charset","UTF-8");
                            connection.setRequestProperty("Content-Type","application/json");
                            connection.setRequestProperty("Authorization",token);
                            OutputStream os=connection.getOutputStream();
                            os.write(jsonAccount.toString().getBytes());
                            os.flush();
                            os.close();
                            if(connection.getResponseCode()==200){
                                InputStream is=connection.getInputStream();
                                BufferedReader reader=new BufferedReader(new InputStreamReader(is));
                                String line;
                                String response="";
                                while ((line=reader.readLine())!=null){
                                    response+=line;
                                }
                                Log.e("MainActivity",response);
                                JSONObject jsonResponse=new JSONObject(response);
                                int code=jsonResponse.getInt("code");
                                String data=jsonResponse.getString("data");
                                JSONObject JsonResponse=new JSONObject(data);
                                String token=JsonResponse.getString("token");
                            }else {
                                Log.e("MainActivity","服务器返回错误！");
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case 5:
                final JSONObject jsonAccount1=new JSONObject();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpURLConnection connection=null;
                        try {
                            URL url=new URL("http://192.168.1.7:9020/iot/class/curtain?action=close&id=2");
                            connection=(HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("POST");
                            connection.setConnectTimeout(5000);
                            connection.setReadTimeout(50000);
                            connection.setRequestProperty("connection","Keep_Alive");
                            connection.setRequestProperty("Charset","UTF-8");
                            connection.setRequestProperty("Content-Type","application/json");
                            connection.setRequestProperty("Authorization",token);
                            OutputStream os=connection.getOutputStream();
                            os.write(jsonAccount1.toString().getBytes());
                            os.flush();
                            os.close();
                            if(connection.getResponseCode()==200){
                                InputStream is=connection.getInputStream();
                                BufferedReader reader=new BufferedReader(new InputStreamReader(is));
                                String line;
                                String response="";
                                while ((line=reader.readLine())!=null){
                                    response+=line;
                                }
                                Log.e("MainActivity",response);
                                JSONObject jsonResponse=new JSONObject(response);
                                String data=jsonResponse.getString("data");
                                JSONObject JsonResponse=new JSONObject(data);
                            }else {
                                Log.e("MainActivity","服务器返回错误！");
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            default:
                break;
        }

    }
}

